package com.tiza.xiaoyoubao.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseActivity;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.AppConfig;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;

import butterknife.BindView;

public class SplashActivity extends BaseActivity {

    @BindView(R.id.rootView)
    View rootView;

    private static final int REQUEST_CODE = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                        Uri.parse("package:" + getPackageName())
                );
                startActivityForResult(intent, REQUEST_CODE);
            }
        }
        // 所需权限
        needPermissions = new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        };
        AlphaAnimation anim = new AlphaAnimation(0.3f, 1.0f);
        anim.setDuration(3000);
        anim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isNeedCheck = true;
                checkPermissions(needPermissions);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        rootView.startAnimation(anim);
    }

    private void doNext() {
        String lastVer = Hawk.get(Constants.KEY_LAST_VER, "0");
        if (AppConfig.hasWelcome && !lastVer.equals("1")) {
            UIHelper.show(mActivity, WelcomeActivity.class, true);
        } else {
            UIHelper.showLogin(SplashActivity.this);
            finish();
        }
    }

    @Override
    protected void permissionOK() {
        super.permissionOK();
        doNext();
    }
}
